package com.hmall.order.utils;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hmall.common.client.FeignItemClient;
import com.hmall.common.pojo.Item;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
public class TestTask {
    @Autowired
    private FeignItemClient itemClient;

    @Autowired
    private OrderService orderService;
    //               秒  分  时 天 月 周
    @Scheduled(cron = "0 */1 * * * ?") // 每隔1分钟
    public void handleTast1(){
        System.out.println("每隔1分钟执行一次 ==>" + LocalDateTime.now());
    }
    //               秒  分  时 天 月 周
    @Scheduled(cron = "*/1 * * * * ?") // 每隔1秒钟
    public void handleTast(){
        //根据付款状态获取订单信息
        //定义订单状态
        Integer status = 1;

        List<Order> orderList = orderService.findOrderList(status);
        //遍历数据
        Date date = new Date();
        long time = date.getTime();

        for (Order order : orderList) {
            long time1 = date.getTime() + 8 * 60 * 60 * 1000;

            long time2 = (time1 - time)/1000/60;
            //判断创建时间
            if (time2 > 1000*60){
                //修改订单状态
                Integer stats = 5;
                orderService.updateStatus(stats,order.getId());
                //获取订单信息
                OrderDetail orderDetil = orderService.getOrderDetil(order.getId());


                UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
                //添加条件
                wrapper.eq("status",1).set("status",5).eq("id",order.getId());

                //补库存
                itemClient.updateStock(orderDetil.getItemId(),orderDetil.getNum());

                //修改订单信息
                boolean update = orderService.update(wrapper);
                System.out.println("修改订单的状态 "+update);
            }

        }

    }
}